extends VBoxContainer

@onready var synergy_manager = get_node("/root/Main/SynergyManager")
@onready var title_label = $TitleLabel
@onready var synergies_container = $SynergiesContainer

func _ready():
	custom_minimum_size = Vector2(200, 100)
	
	if not title_label:
		title_label = Label.new()
		title_label.name = "TitleLabel"
		title_label.text = "Active Synergies"
		title_label.add_theme_font_size_override("font_size", 16)
		title_label.add_theme_color_override("font_color", Color(1, 0.8, 0))
		add_child(title_label)
	
	if not synergies_container:
		synergies_container = VBoxContainer.new()
		synergies_container.name = "SynergiesContainer"
		add_child(synergies_container)
	
	if synergy_manager:
		synergy_manager.synergies_updated.connect(_on_synergies_updated)

func _on_synergies_updated(active_synergies: Dictionary):
	# Clear existing synergy displays
	for child in synergies_container.get_children():
		child.queue_free()
	
	if active_synergies.is_empty():
		var no_synergy_label = Label.new()
		no_synergy_label.text = "No active synergies"
		no_synergy_label.add_theme_font_size_override("font_size", 12)
		no_synergy_label.modulate = Color(0.7, 0.7, 0.7)
		synergies_container.add_child(no_synergy_label)
		return
	
	# Display each active synergy
	for synergy_name in active_synergies:
		var synergy = active_synergies[synergy_name]
		
		var synergy_item = HBoxContainer.new()
		
		# Class icon/indicator
		var class_indicator = ColorRect.new()
		class_indicator.custom_minimum_size = Vector2(8, 8)
		class_indicator.color = get_synergy_color(synergy_name)
		synergy_item.add_child(class_indicator)
		
		# Synergy text
		var synergy_label = Label.new()
		synergy_label.text = "%s (%d): %s" % [synergy_name, synergy.count, synergy.description]
		synergy_label.add_theme_font_size_override("font_size", 12)
		synergy_item.add_child(synergy_label)
		
		synergies_container.add_child(synergy_item)

func get_synergy_color(synergy_name: String) -> Color:
	match synergy_name:
		"Tank":
			return Color(0.3, 0.3, 0.8)
		"Fighter":
			return Color(0.8, 0.3, 0.3)
		"Ranged":
			return Color(0.3, 0.8, 0.3)
		"Mage":
			return Color(0.8, 0.3, 0.8)
		"Assassin":
			return Color(0.5, 0.2, 0.7)
		_:
			return Color.WHITE
